package com.dyuproject.openid.ext;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import com.dyuproject.openid.OpenIdUser;
import com.dyuproject.util.ClassLoaderUtil;
import com.dyuproject.util.http.UrlEncodedParameterMap;

public final class SRegExtension10 extends AbstractExtension {

    public static final class SimpleExchange implements Extension.Exchange {
        private final String _alias;
        private final String _altKey;
        private final String _key;

        public SimpleExchange(String alias) {
            this._alias = alias;
            this._altKey = "openid.sreg." + this._alias;
            this._key = "openid.sreg10." + this._alias;
        }

        public String getAlias() {
            return this._alias;
        }

        public void parseAndPut(OpenIdUser user, HttpServletRequest request,
                Map<String, String> attributes, String extensionAlias) {
            String value = request.getParameter(this._key);
            if (null == value) {
                value = request.getParameter(this._altKey);
            }
            if (value != null)
                attributes.put(this._alias, value);
        }

        public void put(OpenIdUser user, HttpServletRequest request,
                UrlEncodedParameterMap params, String extensionAlias) {
        }
    }

    private static final String __optional;
    private static final Properties __sregConfig = new Properties();
    public static final String ATTR_NAME = "sreg";
    public static final String NAMESPACE = "http://openid.net/sreg/1.0";
    public static final String NS_KEY = "openid.ns.sreg10";
    public static final String OPTIONAL_KEY = "openid.sreg10.optional";

    static {
        try {
            __sregConfig.load(ClassLoaderUtil.getResource(
                    "com/dyuproject/openid/ext/sreg.properties",
                    AxSchemaExtension.class).openStream());

            __optional = (String) __sregConfig.remove("sreg.optional");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> get(OpenIdUser user) {
        return ((Map) user.getAttribute("sreg"));
    }

    public static Map<String, String> remove(OpenIdUser user) {
        return ((Map) user.removeAttribute("sreg"));
    }

    private String _checkKey;
    private final Map<String, Extension.Exchange> _exchanges = new HashMap();

    public SRegExtension10() {
        super("sreg", "http://openid.net/sreg/1.0");
    }

    protected SRegExtension10 addExchange(Extension.Exchange exchange) {
        if (this._exchanges.size() == 0)
            this._checkKey = "openid.sreg10." + exchange.getAlias();
        this._exchanges.put(exchange.getAlias(), exchange);
        return this;
    }

    public SRegExtension10 addExchange(String alias) {
        if (alias == null)
            throw new IllegalArgumentException("alias must be specified.");
        if (!(__sregConfig.containsKey(alias))) {
            throw new IllegalArgumentException("'" + alias
                    + "' not found. The ff are supported:\n" + __optional);
        }
        return addExchange(new SimpleExchange(alias));
    }

    public void onAuthenticate(OpenIdUser user, HttpServletRequest request) {
        String alias = user.getExtension(getNamespace());

        String ns = request.getParameter("openid.ns.sreg");
        if (null == ns) {
            ns = request.getParameter("openid.ns.sreg10");
        }
        if ((alias == null) && (!("http://openid.net/sreg/1.0".equals(ns)))
                && (request.getParameter(this._checkKey) == null)) {
            return;
        }
        Map<String,String> attributes = new HashMap<String,String>(
                new Double(this._exchanges.size() / 0.75D).intValue() + 1);
        user.setAttribute("sreg", attributes);
        for (Extension.Exchange e : this._exchanges.values())
            e.parseAndPut(user, request, attributes, getAlias());
    }

    public void onPreAuthenticate(OpenIdUser user, HttpServletRequest request,
            UrlEncodedParameterMap params) {
        params.put("openid.ns.sreg10", "http://openid.net/sreg/1.0");
        StringBuilder optional = new StringBuilder();
        for (String alias : this._exchanges.keySet()) {
            optional.append(alias).append(',');
        }
        params.put("openid.sreg10.optional",
                optional.substring(0, optional.length() - 1));
    }
}

/* Location:           C:\Users\godin.SHORE\workspace\dyuprojectTest\war\WEB-INF\lib\dyuproject-openid-1.1.7-jarjar.jar
 * Qualified Name:     com.dyuproject.openid.ext.SRegExtension
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.5.3
 */